package cn.zyjw.controller;

import cn.zyjw.domain.Notice;
import cn.zyjw.model.R;
import cn.zyjw.service.NoticeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@Api("公告管理")
@RequestMapping("/notices")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @GetMapping
    @ApiOperation("列表分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页展示条数"),
            @ApiImplicitParam(name = "title", value = "标题"),
            @ApiImplicitParam(name = "startTime", value = "创建时间开始"),
            @ApiImplicitParam(name = "endTime", value = "创建时间结束"),
            @ApiImplicitParam(name = "status", value = "状态"),
    })
    @PreAuthorize("hasAuthority('notice_query')")
    public R<Page<Notice>> listByPage(Page<Notice> page, String title, String startTime, String endTime, Integer status) {
        Page<Notice> noticePage = noticeService.listByPage(page, title, startTime, endTime, status);
        return R.ok(noticePage);
    }

    @PostMapping("/updateStatus")
    @ApiOperation("启用禁用公告管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id"),
            @ApiImplicitParam(name = "status", value = "状态"),
    })
    @PreAuthorize("hasAuthority('notice_update')")
    public R<String> updateStatus(@RequestParam Long id, @RequestParam int status) {
        Notice notice = new Notice();
        notice.setId(id);
        notice.setStatus(status);
        boolean b = noticeService.updateById(notice);
        if (b) {
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }

    @PostMapping("/delete")
    @ApiOperation("公告管理-删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "删除数据id的集合")
    })
    @PreAuthorize("hasAuthority('notice_delete')")
    public R<String> del(@RequestBody Long[] ids) {
        if (ids == null || ids.length == 0) {
            return R.fail("参数不能为空");
        }
        boolean b = noticeService.removeByIds(Arrays.asList(ids));
        if (b) {
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }

}
